<template>
	<view>
		<view class="formBorder">
			<uni-forms class="form" ref="form" :rules="rules" :model="formData">
				<uni-forms-item label="维护日期" labelWidth="180rpx" name="bisWhfaHwrq">
					<uni-easyinput type="text" v-model="formData.bisWhfaHwrq" disabled />
				</uni-forms-item>
				
				<uni-forms-item label="服务时间" labelWidth="180rpx" name="bisWhfaStartTime">
					<view class="fwsj">
						<picker mode="time" :value="formData.bisWhfaStartTime" start="08:00" end="19:00"  @change="bindStartTimeChange">
							<uni-easyinput type="text" disabled v-model="formData.bisWhfaStartTime" placeholder="选择开始时间" />
						</picker>
						<text style="padding: 0px 10px;">-</text>
						<picker mode="time" :value="formData.bisWhfaEndTime" start="10:00" end="21:00" @change="bindEndTimeChange">
							<uni-easyinput type="text" disabled v-model="formData.bisWhfaEndTime" placeholder="选择结束时间" />
						</picker>
					</view>
				</uni-forms-item>
				<uni-forms-item label="服务类型" labelWidth="180rpx" name="bisWhfaFwlx">
					<uni-data-checkbox mode="tag" v-model="formData.bisWhfaFwlx" :localdata="fwlx" disabled></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="服务项目" labelWidth="180rpx" name="whfaFfxesChoose">
					<view class="" v-if="!formData.bisWhfaFwlx">请选择服务类型</view>
					<uni-data-checkbox mode="tag" multiple v-model="formData.whfaFfxesChoose" :localdata="liveProjectArr" @change="changeFwxm"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="服务时长(分钟)" labelWidth="180rpx">
					<uni-easyinput type="text" disabled v-model="fwsc" placeholder="请选择服务项目" />
				</uni-forms-item>
				<button class="subBtn" @click="submitForm">保存</button>
			</uni-forms>
		</view>
	</view>
</template>

<script lang="ts">
	import { route,routeMixin } from "@/route/Route"
	import { planService } from "@/service/PlanService"
	
	export default {
		mixins: [routeMixin],
		data() {
			return {
				currentDate: "",
				dyjsrq: "",
				dykkrq: "",
				formData:{
					bisWhfaEndTime: "", 
					bisWhfaHwrq: "", 
					bisWhfaStartTime: "", 
					bisWhfaZhsqId: 0, 
					bz: "", 
					bisWhfaFwlx: "",
					whfaFfxesChoose: [],
					whfaFfxes: []
				},
				fwxm:[],
				fwlx:[],
				fwsc: 0,
				liveProjectArr: [],
				rules: {
					bisWhfaStartTime: {
						rules:[
							{
								required: true,
								errorMessage: '请选择开始时间',
							}
						],
						label:'开始时间'
					},
					bisWhfaEndTime: {
						rules:[
							{
								required: true,
								errorMessage: '请选择结束时间',
							}
						],
						label:'结束时间'
					},
					whfaFfxesChoose: {
						rules:[
							{
								required: true,
								errorMessage: '请选择服务项目',
							}
						],
						label:'择服务项目'
					}
				},
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: "修改维护方案"
			})
		},
		methods: {
			routeParameterEvent(data) {
				
				this.formData = data.data
				this.routeParameter = data
				console.log("edit",data)
				
				planService.fwxm((res)=>{
					console.log(res)
					this.fwxm = res
					for(let type of this.fwxm){
						// this.fwlx = [...this.fwlx,{text: type.fwlxname,value:type.fwlxcode}]
						switch(type.fwlxcode){
							case '01' : 
								type.fwlxcode = '94'
								break
							case '04' :
								type.fwlxcode = '95'
								break
							default:
								
						}
						let children = []
						for(let xm of type.fwxmList){
							xm.text = xm.bisYmkdzId
							xm.value = xm.bisYmkdzMc
							children.push({text:xm.bisYmkdzMc,value:xm.bisYmkdzId})
						}
						this.fwlx = [...this.fwlx,{text: type.fwlxname,value:type.fwlxcode,children:children}]
						console.log(this.fwlx)
						// this.liveProjectArr=[...this.liveProjectArr,{text:type.fwlxname,value:type.fwlxcode,children:children}]
					}
					this.liveProjectArr = []
					this.formData.whfaFfxesChoose = []
					for(let fwlxItem of this.fwlx){
						if(this.formData.bisWhfaFwlx === fwlxItem.value){
							this.liveProjectArr=fwlxItem.children
						}
					}
					this.formData.whfaFfxesChoose = []
					for(let item of this.formData.whfaFfxes){
						this.formData.whfaFfxesChoose.push(item.bisWhfaDzid)
					}
					this.changeFwxm()
				})
			},
			
			bindStartTimeChange(e) {
				this.formData.bisWhfaStartTime = e.detail.value
				let startTime =  new Date(this.formData.bisWhfaHwrq+" "+this.formData.bisWhfaStartTime+":00")
				let endHours = startTime.getHours() + 2
				if(endHours < 10){
					endHours = "0"+endHours
				}
				let endMinutes = startTime.getMinutes()
				if(endMinutes < 10){
					endMinutes = "0"+endMinutes
				}
				this.formData.bisWhfaEndTime = endHours+":"+endMinutes


			},
			bindEndTimeChange(e) {
				this.formData.bisWhfaEndTime = e.detail.value
			},
			submitForm(){
				console.log(this.formData)
				let _this = this
				this.$refs.form.validate().then(res=>{
					uni.showModal({
						title: '提示',
						content: '是否确认提交',
						success: function (res) {
							if (res.confirm) {
								_this.formData.whfaFfxes = []
								for(let item of _this.formData.whfaFfxesChoose){
									_this.formData.whfaFfxes.push({bisWhfaDzid:item})
								}
								console.log(_this.formData)
								planService.updateZhfa(_this.formData,(resp)=>{
									console.log(resp)
									route.goBack()
								})
							} else if (res.cancel) {
							}
						}
					});
				}).catch(err =>{
					// console.log('表单错误信息：', err);
				})
				
				
			},
			changeFwxm(){
				console.log(this.formData.whfaFfxesChoose)
				console.log(this.fwxm)
				this.fwsc = 0
				for(let item of this.fwxm){
					if(item.fwlxcode === this.formData.bisWhfaFwlx){
						for(let item2 of item.fwxmList){
							if(this.formData.whfaFfxesChoose.indexOf(item2.bisYmkdzId) > -1){
								this.fwsc = this.fwsc + (item2.bisYmkdzFwsc - 0)
							}
						}
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
.formBorder{
	display: flex;
	flex-direction: column;
	justify-content: center;
	background-color: $uni-bg-color-grey;
	background: white;
	margin: 40rpx;
}
.form{
}
.subBtn{
	background-color: $uni-color-primary;
	color: $uni-text-color-inverse;
}
.fwsj{
	display: flex;
	justify-content: center;
	align-items: center;
}
.fwxmIconView{	
	display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}
.dateTags{
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	justify-content: flex-start;
}
.dateTag{
	margin: 10rpx;
}
</style>
